﻿/**
 * \brief A class representing the splash screen
 * This screen will fade textures in and out until all textures have been displayed
 * After that, it will go to the title screen
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace KaoticSSB.Menus
{
	public class SplashScreen : MonoBehaviour 
	{
		// Editor Variables
		public const float TimePerImage = 2.0f;
		public const float FadeTimePerImage = 3.0f;
		public const float InitialDelay = 3.0f;
		public List<Texture> Textures;

		public void Start()
		{
			mCurrentTimeOnImage = TimePerImage;
			mCurrentInitialDelay = InitialDelay;
			mCurrentImageIndex = 0;
			mCurrentOpacity = 0.0f;
			mWaitingForInitialDelay = true;
			StartFadeIn ();
		}

		public void Update()
		{
			mCurrentTimeOnImage -= Time.deltaTime;
			mCurrentInitialDelay -= Time.deltaTime;

			//keep screen empty for a specified period of time
			if(mWaitingForInitialDelay && mCurrentInitialDelay <= 0.0f)
			{
				StartFadeIn ();
				mWaitingForInitialDelay = false;
			}

			//core image swap loop after initial delay has passed
			if(!mWaitingForInitialDelay)
			{
				if(mFadingOut)
				{
					FadeOut ();
				}
				else if(mFadingIn)
				{
					FadeIn ();
				}
				else if(mCurrentTimeOnImage <= 0.0f || IsSkipPressed())
				{
					SwapToNextImage();
				}
			}
		}

		public void OnGUI()
		{
			//Display background texture on whole screen
			if(Textures.Count > 0)
			{
				Color color = GUI.color;
				color.a = mCurrentOpacity;
				GUI.color = color;
				GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Textures[mCurrentImageIndex]);
			}
		}

		private bool IsSkipPressed()
		{
			return Input.GetButtonDown("Action_Button") || Input.GetButtonDown("Start_Button");
		}

		private bool SwapToNextImage()
		{
			StopFadeOut();
			if(mCurrentImageIndex+1 == Textures.Count)
			{
				Application.LoadLevel ("TitleScene");
				return false;
			}
			else
			{
				mCurrentImageIndex++;
				StartFadeIn();
				return true;
			}
		}
		private void StartFadeIn()
		{
			mFadingIn = true;
			mCurrentFadeInTime = FadeTimePerImage;
		}
		private void StopFadeIn()
		{
			mFadingIn = false;
			mCurrentTimeOnImage = TimePerImage;
		}
		private void StartFadeOut()
		{
			mFadingOut = true;
			mCurrentFadeOutTime = FadeTimePerImage;
		}
		private void StopFadeOut()
		{
			mFadingOut = false;
			mCurrentOpacity = 0.0f;
		}
		private void FadeIn()
		{
			mCurrentFadeInTime -= Time.deltaTime;
			mCurrentOpacity = (FadeTimePerImage - mCurrentFadeInTime) / FadeTimePerImage;
			if(IsSkipPressed())
			{
				SwapToNextImage();
			}
			else if(mCurrentFadeInTime <= 0.0f)
			{
				StopFadeIn ();
			}
		}
		private void FadeOut()
		{
			mCurrentFadeOutTime -= Time.deltaTime;
			mCurrentOpacity = mCurrentFadeOutTime / FadeTimePerImage;
			if(mCurrentFadeOutTime <= 0.0f)
			{
				SwapToNextImage();
			}
		}

		// State variables
		private float 	mCurrentTimeOnImage;
		private int 	mCurrentImageIndex;
		private bool 	mFadingIn;
		private float 	mCurrentFadeInTime;
		private bool 	mFadingOut;
		private float 	mCurrentFadeOutTime;
		private float 	mCurrentOpacity;
		private float 	mCurrentInitialDelay;
		private bool 	mWaitingForInitialDelay;
	}
}